#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>


#define GPIOLED_CNT     1           /* 设备号个数 */
#define GPIOLED_NAME    "gpioled"   /* 名字 */
#define LEDOFF          1          /* 关灯 */
#define LEDON           0           /* 开灯 */



struct gpioled_dev
{
    dev_t devid;    /*  设备号 */
    struct cdev cdev; /* cdev */
    struct class *class; /* 类 */
    struct device *device; /* 设备 */
    int major;             /* 主设备号 */
    int minor;              /* 次设备号 */
    struct device_node *nd; /* 设备节点 */
    int led_gpio; /* led所使用的GPIO编号 */
    int dev_status; /* 设备状态，0：设备未使用；>0：设备已经被使用 */
    struct mutex lock; /* 互斥锁 */
};


struct gpioled_dev gpioled;    /* led设备 */


static int led_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &gpioled;
    if(mutex_lock_interruptible(&gpioled.lock)){
        return -ERESTARTSYS;
    }
#if 0
    mutex_lock(&gpioled.lock);  /*  上锁 */
#endif
    return 0;
}

static ssize_t led_read(struct file *filp, const char __user *buf,size_t cnt,loff_t *offt)
{
    return 0;
}


static ssize_t led_write(struct file *filp, const char __user *buf,size_t cnt,loff_t *offt)
{
    int retval;
    unsigned char databuf[1];
    unsigned char ledstate;

    struct gpioled_dev *dev = filp->private_data;

    retval = copy_from_user(databuf,buf,cnt);

    ledstate = databuf[0];

    if(ledstate == LEDOFF){
        gpio_set_value(dev->led_gpio,0);
    }else if(ledstate == LEDON){
        gpio_set_value(dev->led_gpio,1);
    }
    return retval;
}

static int led_release(struct inode *inode, struct file *filp)
{
    struct gpioled_dev *dev = filp->private_data;
    mutex_unlock(&dev->lock);  /* 解锁 */
    return 0;
}

/* 设备操作函数 */
static struct file_operations gpioled_fops = {
    .owner	= THIS_MODULE,
    .open = led_open,
    .read = led_read,
    .write = led_write,
    .release = led_release
};

static int __init led_init(void)
{
    int ret = 0;

    mutex_init(&gpioled.lock);  /* 初始化互斥锁 */
    /* 设置LED所使用的GPIO */
    /* 1、获取设备节点 gpioled*/
    gpioled.nd = of_find_node_by_path("/gpioled");
    if (gpioled.nd != NULL)
    {
        printk("gpioled node has been found!\r\n");
    }

    /* 2、 */
    gpioled.led_gpio = of_get_named_gpio(gpioled.nd,"led-gpio",0);
    ret = gpio_direction_output(gpioled.led_gpio,1);


    /* 注册字符设备驱动 */
    /* 1、创建设备号 */
    if(gpioled.major){
        gpioled.devid = MKDEV(gpioled.major,0);
        register_chrdev_region(gpioled.devid,GPIOLED_CNT,GPIOLED_NAME);
    }else{
        alloc_chrdev_region(&gpioled.devid,0,GPIOLED_CNT,GPIOLED_NAME);   /* 申请设备号 */
        gpioled.major = MAJOR(gpioled.devid);    /* 获取分配的主设备号 */
        gpioled.minor = MINOR(gpioled.devid);   /*  获取分配的次设备号 */
    }

    printk("gpioled major = %d, minor = %d \r\n",gpioled.major,gpioled.minor);


    /*  2、初始化 */
    gpioled.cdev.owner = THIS_MODULE;
    cdev_init(&gpioled.cdev,&gpioled_fops);

    /*  3、添加一个cdev */
    cdev_add(&gpioled.cdev,gpioled.devid,GPIOLED_CNT); 

    /* 创建类 */
    gpioled.class = class_create(THIS_MODULE,GPIOLED_NAME);
    if(IS_ERR(gpioled.class)){
        return PTR_ERR(gpioled.class);
    }

    /*  5、创建设备 */
    gpioled.device = device_create(gpioled.class,NULL,gpioled.devid,NULL,GPIOLED_NAME);

    if(IS_ERR(gpioled.device)) {
        return  PTR_ERR(gpioled.device);
    }

    return 0;
}

static void __exit led_exit(void)
{
    /*  注销字符设备驱动 */
    cdev_del(&gpioled.cdev);
    unregister_chrdev_region(gpioled.devid,GPIOLED_CNT);

    device_destroy(gpioled.class,gpioled.devid);
    class_destroy(gpioled.class);
}






module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chaoliang");
